package com.snowfield.lb47.lbconfig.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowfield.lb47.lbconfig.domain.FrontProxyCrt;
import com.snowfield.lb47.lbconfig.service.IFrontProxyCrtService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 代理证书Controller
 *
 * @author snowfield
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/lbconfig/proxyCrt")
public class FrontProxyCrtController extends BaseController {
    @Autowired
    private IFrontProxyCrtService frontProxyCrtService;

    private static final Logger LOGGER = LoggerFactory.getLogger(FrontProxyCrtController.class);

    /**
     * 查询代理证书列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:list')")
    @GetMapping("/list")
    public TableDataInfo list(FrontProxyCrt frontProxyCrt) {
        startPage();
        List<FrontProxyCrt> list = frontProxyCrtService.selectFrontProxyCrtList(frontProxyCrt);
        return getDataTable(list);
    }

    /**
     * 导出代理证书列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:export')")
    @Log(title = "代理证书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FrontProxyCrt frontProxyCrt) {
        List<FrontProxyCrt> list = frontProxyCrtService.selectFrontProxyCrtList(frontProxyCrt);
        ExcelUtil<FrontProxyCrt> util = new ExcelUtil<FrontProxyCrt>(FrontProxyCrt.class);
        util.exportExcel(response, list, "代理证书数据");
    }

    /**
     * 获取代理证书详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(frontProxyCrtService.selectFrontProxyCrtById(id));
    }

    /**
     * 新增代理证书
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:add')")
    @Log(title = "代理证书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FrontProxyCrt frontProxyCrt) {
        if (!frontProxyCrtService.checkNameUnique(frontProxyCrt)) {
            return AjaxResult.error("新增代理证书 '" + frontProxyCrt.getCrtName() + "'失败，名称已存在");
        }
        frontProxyCrt.setCreateBy(getUsername());
        if ("create".equals(frontProxyCrt.getCreateType())) {
            return AjaxResult.error("后台【生成证书】功能未生效！如有需要请联系作者QQ:3961919621");
        }
        return toAjax(frontProxyCrtService.insertFrontProxyCrt(frontProxyCrt));
    }

    /**
     * 修改代理证书
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:edit')")
    @Log(title = "代理证书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FrontProxyCrt frontProxyCrt) {
        if (!frontProxyCrtService.checkNameUnique(frontProxyCrt)) {
            return AjaxResult.error("修改代理证书 '" + frontProxyCrt.getCrtName() + "'失败，名称已存在");
        }
        frontProxyCrt.setCreateBy(getUsername());
        if ("create".equals(frontProxyCrt.getCreateType())) {
            return AjaxResult.error("后台【生成证书】功能未生效！如有需要请联系作者QQ:3961919621");
        }
        return toAjax(frontProxyCrtService.updateFrontProxyCrt(frontProxyCrt));
    }

    /**
     * 删除代理证书
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:remove')")
    @Log(title = "代理证书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(frontProxyCrtService.deleteFrontProxyCrtByIds(ids));
    }

    /**
     * 下载代理证书文件
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:proxyCrt:export')")
    @Log(title = "代理证书", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadCrt/{id}")
    public void downloadCrt(HttpServletResponse response, @PathVariable("id") Long id) {
        FrontProxyCrt frontProxyCrt = frontProxyCrtService.selectFrontProxyCrtById(id);
        if (frontProxyCrt != null) {
            File crtFile = new File(frontProxyCrt.getCrtPath());
            if (crtFile.exists()) {
                try {
                    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                    response.setCharacterEncoding("utf-8");
                    FileUtils.copyFile(crtFile, response.getOutputStream());
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        }
    }

}
